﻿using UnityEngine;

namespace YeqianFramework.core
{
	/// <summary>
	/// 单例继承这个类，自动在需要的时候创建游戏物体
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public abstract class MonoBehaviourSingleton<T> : MonoBehaviour where T : MonoBehaviour
	{
		public MonoBehaviourSingleton()
		{
			Init();
		}

		private static T current;
		public static T Current
		{
			get
			{
				if (current != null) return current;

				GameObject go = new GameObject();
				current = go.AddComponent<T>();
				DontDestroyOnLoad(go);

				return current;
			}
		}

		/// <summary>
		/// 初始化操作，被构造方法调用
		/// </summary>
		protected abstract void Init();

		protected virtual void Awake()
		{
			gameObject.name = GetType().Name;
		}
	}

}